<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="管理员名">
        <el-input v-model="form.username" placeholder="请输入用户名" readonly></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.mail" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input type="password" v-model="form.password2" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择">
          <el-option v-for="(role, key) in roles" :key="key" :label="role" :value="key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="管理员状态">
        <el-radio-group v-model="form.status">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">冻结</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <!-- <el-button @click="onCancel">取消</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getOne, postOne } from '@/api/admin'
import { getOpt } from '@/api/role'

export default {
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        password: '',
        password2: '',
        mail: '',
        role: '',
        status: 0
      },
      roles: {}
    }
  },
  created() {
    getOpt(opt => {
      this.roles = opt
    })
    getOne({
      _id: this.$route.query._id
    }).then(info => {
      Object.assign(this.form, info.data)
    })
  },
  methods: {
    onSubmit() {
      var post = Object.assign({}, this.form)
      delete post.password2
      postOne({
        _id: this.form._id
      }, post).then(res => {
        if (res.code === 30000) {
          this.$message({
            type: 'success',
            message: '更新成功!'
          })
          this.$router.replace('/admin/list')
        } else {
          this.$message('更新失败!')
        }
      })
    },
    onCancel() {
      this.$message({
        message: '已取消添加!',
        type: 'warning'
      })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
